﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RondoDance.OM;
using System.Web.UI.HtmlControls;

[System.ComponentModel.ComplexBindingProperties("DataSource")]
public partial class CategoriesTreeControl : System.Web.UI.UserControl
{
    private string _categoryNavigateUrlFormat = "#{0}";
    private string _pageNavigateUrlFormat = "#{0}";

    public IEnumerable<StaticPage> DataSource { get; set; }

    public string CategoryNavigateUrlFormat
    {
        get { return _categoryNavigateUrlFormat; }
        set { _categoryNavigateUrlFormat = value; }
    }

    public string PageNavigateUrlFormat
    {
        get { return _pageNavigateUrlFormat; }
        set { _pageNavigateUrlFormat = value; }
    }

    public override void DataBind()
    {
        base.OnDataBinding(EventArgs.Empty);

        HtmlGenericControl list = new System.Web.UI.HtmlControls.HtmlGenericControl("ul");

        foreach (StaticPage item in DataSource)
        {
            CreateListItem(list, item);
        }

        plhTree.Controls.Add(list);
    }

    private void CreateListItem(HtmlGenericControl list, StaticPage page)
    {
        HtmlGenericControl listItem = new HtmlGenericControl("li");
        // listItem.Attributes.Add("class", "category");
        listItem.Controls.Add(new HyperLink()
        {
            NavigateUrl = string.Format(CategoryNavigateUrlFormat, page.Id),
            Text = page.PageTitle
        });

        list.Controls.Add(listItem);
    }

    private HtmlGenericControl CreateHyperLinkItem(string title, string href, string cssClass)
    {
        HtmlGenericControl listItem = new HtmlGenericControl("li");
        listItem.Attributes.Add("class", cssClass);

        listItem.Controls.Add(new HyperLink()
        {
            NavigateUrl = href,
            Text = title
        });

        return listItem;
    }
}
